x86/amd: Introduce and use X86_BUG_NULL_SEG
authorAndrew Cooper <andrew.cooper3@citrix.com>
Thu, 27 Dec 2018 15:13:58 +0000 (15:13 +0000)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Fri, 17 Sep 2021 12:27:06 +0000 (13:27 +0100)
AMD/Hygon processors before the Zen2 microarchitecture don't clear the base or
limit fields when loading a NULL segment.

Express the logic in terms of cpu_bug_null_seg, and adjust the workaround in
do_set_segment_base().

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
xen/arch/x86/cpu/amd.c
xen/arch/x86/cpu/hygon.c
xen/arch/x86/pv/misc-hypercalls.c
xen/include/asm-x86/cpufeature.h
xen/include/asm-x86/cpufeatures.h

index c4d84373a7105fdb3b6f4a4c7c437ff6642c6edb..f87484b7ce61dd396c9ab7ac93d6f397be69aa10 100644 (file)
@@ -801,6 +801,13 @@ static void init_amd(struct cpuinfo_x86 *c)
            c->x86 == 0x17)
                detect_zen2_null_seg_behaviour();
 
+       /*
+        * AMD CPUs before Zen2 don't clear segment bases/limits when loading
+        * a NULL selector.
+        */
+       if (c == &boot_cpu_data && !cpu_has_nscb)
+               setup_force_cpu_cap(X86_BUG_NULL_SEG);
+
        /* MFENCE stops RDTSC speculation */
        if (!cpu_has_lfence_dispatch)
                __set_bit(X86_FEATURE_MFENCE_RDTSC, c->x86_capability);
index 429d6601fc13d1dabcf00732682ad8fc8fe4fd10..cdc94130dd2e48b112a76a8f14032e6ae86d4442 100644 (file)
@@ -40,6 +40,13 @@ static void init_hygon(struct cpuinfo_x86 *c)
            c->x86 == 0x18)
                detect_zen2_null_seg_behaviour();
 
+       /*
+        * Hygon CPUs before Zen2 don't clear segment bases/limits when
+        * loading a NULL selector.
+        */
+       if (c == &boot_cpu_data && !cpu_has_nscb)
+               setup_force_cpu_cap(X86_BUG_NULL_SEG);
+
        /* MFENCE stops RDTSC speculation */
        if (!cpu_has_lfence_dispatch)
                __set_bit(X86_FEATURE_MFENCE_RDTSC, c->x86_capability);
index 3a4e4aa4603e720928233b680e004e40838eeb71..5dade2472687266d01b8879d52de7445359bb483 100644 (file)
@@ -227,8 +227,7 @@ long do_set_segment_base(unsigned int which, unsigned long base)
         if ( sel > 3 )
             /* Fix up RPL for non-NUL selectors. */
             sel |= 3;
-        else if ( boot_cpu_data.x86_vendor &
-                  (X86_VENDOR_AMD | X86_VENDOR_HYGON) )
+        else if ( cpu_bug_null_seg )
             /* Work around NUL segment behaviour on AMD hardware. */
             asm volatile ( "mov %[sel], %%gs"
                            :: [sel] "r" (FLAT_USER_DS32) );
index 94a485f99c226f41c642126010ab471a5b862767..802d9257b0bf93bec192758a75147482ad2a0f30 100644 (file)
 
 /* Bugs. */
 #define cpu_bug_fpu_ptrs        boot_cpu_has(X86_BUG_FPU_PTRS)
+#define cpu_bug_null_seg        boot_cpu_has(X86_BUG_NULL_SEG)
 
 enum _cache_type {
     CACHE_TYPE_NULL = 0,
index 6c8f432aee4fcb9ac7d341a79e68de3293470e04..72beb7babcce2414b9042b3ebd2e844678a7a504 100644 (file)
@@ -45,6 +45,7 @@ XEN_CPUFEATURE(XEN_SHSTK,         X86_SYNTH(26)) /* Xen uses CET Shadow Stacks *
 #define X86_BUG(x) ((FSCAPINTS + X86_NR_SYNTH) * 32 + (x))
 
 #define X86_BUG_FPU_PTRS          X86_BUG( 0) /* (F)X{SAVE,RSTOR} doesn't save/restore FOP/FIP/FDP. */
+#define X86_BUG_NULL_SEG          X86_BUG( 1) /* NULL-ing a selector preserves the base and limit. */
 
 /* Total number of capability words, inc synth and bug words. */
 #define NCAPINTS (FSCAPINTS + X86_NR_SYNTH + X86_NR_BUG) /* N 32-bit words worth of info */